//
//  LGPhotoReportDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/8.
//

#import "LGPhotoReportDetailController.h"
#import "LGCheckPhotoTool.h"
@interface LGPhotoReportDetailController ()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , assign)CGFloat photoW;
/**   */
@property (nonatomic , strong)NSMutableArray *picArray;
/**   */
@property (nonatomic , strong)NSMutableArray *urlArray;
/**   */
@property (nonatomic , strong)NSMutableArray *fileArray;
/**   */
@property (nonatomic , strong)NSMutableArray *buttonArray;
@end

@implementation LGPhotoReportDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"图片报告";
    [self.view addSubview:self.titleLabel];
    NSInteger count = isPad?8:4;
    self.photoW = (CGFloat)(Screen_W-viewPix(32)-viewPix(12)*(count-1))/count;
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.navigationView.title = LGNSString(dataDic[@"type_name"]);
    self.titleLabel.text = [NSString stringWithFormat:@"%@ 上传",dataDic[@"date"]];
    NSArray *itemArray = dataDic[@"items"];
    NSMutableArray *imageArray = [NSMutableArray array];
    NSMutableArray *fileArray = [NSMutableArray array];
    for (NSDictionary *tempDic in itemArray) {
        if ([tempDic.allKeys containsObject:@"imgid"]) {
            //图片
            [imageArray addObject:tempDic];
        }else{
            //文件
            [fileArray addObject:tempDic];
        }
    }
    self.fileArray = fileArray;
    CGFloat top = [self createPhotoView:imageArray];
    [self createFileView:fileArray top:top];
   
}

-(void)checkFileAction:(UIButton *)sender{
    NSInteger index = sender.tag - 2000;
    NSDictionary *tempDic = self.fileArray[index];
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.urlStr = LGNSString(tempDic[@"file_url"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(CGFloat)createPhotoView:(NSArray *)photoArray{
    CGFloat top = viewPix(45)+topBarHeight;
    for (UIImageView *imageView in self.picArray) {
        [imageView removeFromSuperview];
    }
    [self.picArray removeAllObjects];
    self.urlArray = [NSMutableArray array];
    CGFloat marginX = viewPix(16);
    CGFloat marginY = viewPix(45)+topBarHeight;
    NSInteger count = isPad?8:4;
    CGFloat photoW = (CGFloat)(Screen_W-viewPix(32)-viewPix(12)*(count-1))/count;
    for (NSInteger i=0; i<photoArray.count; i++) {
        NSString *urlStr = LGNSString(photoArray[i][@"url"]);
        [self.urlArray addObject:urlStr];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(marginX+(photoW+viewPix(12))*(i%count), marginY+(photoW+viewPix(12))*(i/count), photoW, photoW)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        imageView.userInteractionEnabled = YES;
        imageView.cornerRidus = 3;
        imageView.tag = 1000+i;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkBigPhotoAction:)]];
        [self.view addSubview:imageView];
        [self.picArray addObject:imageView];
        top = kMaxY(imageView.frame) + viewPix(16);
    }

    return top;
}

-(void)createFileView:(NSArray *)fileArray top:(CGFloat)top{
    for (UIButton *button in self.buttonArray) {
        [button removeFromSuperview];
    }
    [self.buttonArray removeAllObjects];
    
    for (NSInteger i=0; i<fileArray.count; i++) {
        NSDictionary *tempDic = fileArray[i];
        UIButton *button = [UIButton buttonWithTitle:LGNSString(tempDic[@"title"]) titleFont:LGFont(15) textColor:@"#333333" frame:CGRectMake(viewPix(16), top+viewPix(32)*i, Screen_W-viewPix(32), viewPix(32))];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.tag = 2000+i;
        [button addTarget:self action:@selector(checkFileAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [self.buttonArray addObject:button];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        [button addSubview:tipView];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(button);
            make.width.height.equalTo(@(16));
            make.centerY.equalTo(button);
        }];
    }
    
}

-(void)countButtonAction{
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"图片报告";
    bigPhotoView.picArray = self.urlArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:0];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}


-(void)checkBigPhotoAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView = (UIImageView *)sender.view;
    NSInteger index = imageView.tag-1000;
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"图片报告";
    bigPhotoView.picArray = self.urlArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:index];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(12)+topBarHeight, Screen_W-viewPix(32), viewPix(22));
    }
    return _titleLabel;
}


-(NSMutableArray *)picArray{
    if(!_picArray){
        _picArray = [NSMutableArray array];
    }
    return _picArray;
}

-(NSMutableArray *)fileArray{
    if(!_fileArray){
        _fileArray = [NSMutableArray array];
    }
    return _fileArray;
}

-(NSMutableArray *)buttonArray{
    if(!_buttonArray){
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
